<?php


namespace app\api\controller;

use think\Db;
use think\Exception;

/**
 * Class Pk包
 * @package app\api\controller
 */
class Pk extends \app\common\controller\Api
{
    protected $noNeedRight = ['*'];
    /**
     * 轮播图
     */
    public function banner(){
        $list = db('banner_pk')
            ->field('id,image')
            ->order('id DESC')//倒叙
            ->select();
        $this->success('ok',$list);
    }
    /**
     * 获取运行中的矿机
     */
    public function get_ing_list(){
        $user = $this->auth->getUser();
//        if ($user->is_truename == 1){
//            $this->error('请先通过实名验证');
//        }
//$a = db('user_mill')->where('user_id',$user->id)->select();
        $list = model('user_mill')
            ->with('cbmill')
            ->where('user_id',$user->id)
            ->where(function ($q){
                $q->whereOr('status','1')->whereOr('is_receive','1');
            })
            ->order('price DESC')
            ->select();
//        halt(collection($list)->toArray());
//        halt($a);
        $data = [];
        foreach ($list as $key => $value){
            $data[$key]['id'] = $value['id'];
            $data[$key]['image'] = $value['cbmill']['image'];//图片
            $data[$key]['name'] = $value['cbmill']['name'];
            $data[$key]['cycle'] = $value['numcount'];//总周期
            $data[$key]['receive_num'] = $value['receive_num'];//已产比次数
            $data[$key]['production'] = $value['cbmill']['production'];//产量
            $data[$key]['shengyu_zhouqi'] = $value['numcount'] - $value['receive_num'];
            $data[$key]['ci_production'] = round($value['cbmill']['production']/$value['numcount'],2);//单次产量
            $data[$key]['qian'] = $value['is_receive'] == 1 ? date('Y-m-d') == date('Y-m-d',$value['termtime'])? true :false : false;
        }
        $count = db('user_mill')
            ->where('user_id',$user->id)
            ->where('is_receive','1')
            ->whereTime('termtime','d')
            ->sum('one_make');//所有可领取的数量
        if($count > 0){
            $info['one_key'] = true;
        }else{
            $info['one_key'] = false;
        }
        $info['list'] = $data;
        $this->success('ok',$info);
    }

    /**
     * 签到
     */
    public function qiandao(){
        $mill_id = input('mill_id') ? input('mill_id'):'';//不传就所有
        $user = $this->auth->getUser();
        if ($user->is_truename == 1){
            $this->error('请先通过实名验证');
        }
        $where = [];
        if ($mill_id){
            $where['id'] = $mill_id;
        }
        $where['user_id'] = $user->id;
        Db::startTrans();
        try {
             $count = db('user_mill')
                 ->where($where)
                 ->whereTime('termtime','d')//今天产出的
                 ->where('is_receive','1')//可领的
                 ->sum('one_make');//所有可领取的数量
            $list = db('user_mill')
                ->where($where)
                ->whereTime('termtime','d')//今天产出的
                ->where('is_receive','1')//可领的
                ->select();
            foreach ($list as $key => $value){
                if ($value['numcount'] == $value['receive_num']){
                    db('user_mill')->where('id',$value['id'])->update(['status'=>3,'is_receive'=>2,'updatetime'=>time()]);
                }else{
                    db('user_mill')->where('id',$value['id'])->update(['is_receive'=>2,'updatetime'=>time()]);
                }
            }
            $user->cb_num += $count;
            $user->save();
            setCbwater($user->id,$count,1,'签到领取矿机产出');

            Db::commit();
        }catch (Exception $exception){
            Db::rollback();
            $this->error($exception->getMessage());
        }
        $this->success('ok');
    }

    /**
     * pk包兑换列表
     */
    public function pkbage(){
        $list = model('cb_mill')
            ->where('id','>',1)
            ->order('production ASC')//产量正序
            ->select();
        foreach ($list as  $key => $value){
            $list[$key]['ci_production'] = round($value['production']/$value['cycle']/24,2);//单次产量
        }
        $user = $this->auth->getUser();
        $data = [];
        $data['yue'] = $user->cb_num;
        $data['list'] = $list;
        $this->success('ok',$data);
    }

    /**
     * 兑换pk包
     */
    public function duihuanpk(){
        $pk_id = input('pk_id') ? input('pk_id') : $this->error('未指定pk包');
        $user = $this->auth->getUser();
        if ($user->is_truename == 1){
            $this->error('请先通过实名验证');
        }
        $pk = db('cb_mill')->find($pk_id);
        if (!$pk){
            $this->error('pk包指定错误');
        }
        if ($pk['min_level'] > $user->level){
            $this->error('等级不足,无法兑换');
        }
        if ($user->cb_num < $pk['price']){
            $this->error('余额不足');
        }
        $havenum = db('user_mill')
            ->where('user_id',$user->id)
            ->where('cb_mill_id',$pk_id)
            ->where('status','1')
            ->count();//统计该pk包已经持有运行的数量
        if ($havenum >= $pk['max_num']){
            $this->error('已持有最大同时运行量,暂不可继续兑换');
        }
        //开始兑换
        Db::startTrans();
        try {
            $user->cb_num -= $pk['price'];
            $user->cc_num += $pk['cc_reward'];
            $user->save();
            setCbwater($user->id,$pk['price'],2,'兑换pk包扣除');
            setCcwater($user->id,$pk['cc_reward'],1,'复投pk包奖励');
            $data = [];
            $data['user_id'] = $user->id;
            $data['cb_mill_id'] = $pk_id;
            $data['price'] = $pk['price'];
            $data['receive_num'] = 0;
            $data['status'] = 1;
            $data['cb_num'] = 0;
            $data['is_receive'] = 2;
            $data['createtime'] = time();
            $data['numcount'] = $pk['cycle'];
            $data['one_make'] = $pk['production']/$pk['cycle'];//单次产量
            $data['termtime'] = time();
            db('user_mill')->insert($data);

            futou_send_cc($user->id,$pk['cc_reward']);//复投奖励

            Db::commit();
        }catch (Exception $exception){
            Db::rollback();
            $this->error($exception->getMessage());
        }
        $this->success('兑换成功');
    }
}